package com.gscitysfy.cus.drain.modules.duty.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gscitysfy.cloudfrmwk.assistant.domain.Result;

import com.gscitysfy.cus.drain.modules.duty.entity.MoperDutyMnClasses;
import com.gscitysfy.cus.drain.modules.duty.entity.MoperDutyMnExchange;
import com.gscitysfy.cus.drain.modules.duty.entity.MoperDutyMnExchangeExcel;
import com.gscitysfy.cus.drain.modules.duty.entity.MoperDutyMnRota;
import com.gscitysfy.cus.drain.modules.duty.mapper.MoperDutyMnExchangeMapper;
import com.gscitysfy.cus.drain.modules.duty.mapper.MoperDutyMnRotaMapper;
import com.gscitysfy.cus.drain.modules.duty.service.IMoperDutyMnClassesService;
import com.gscitysfy.cus.drain.modules.duty.service.IMoperDutyMnExchangeService;
import com.gscitysfy.cus.drain.modules.duty.service.IMoperDutyMnRotaService;
import com.gscitysfy.cus.drain.modules.duty.vo.MoperDutyMnExchangeVo;
import com.gscitysfy.cus.drain.utils.HttpResponseUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 葛守跃
 * @version 1.0.0
 * @ClassName MoperDutyMnExchangeServiceImpl
 * @Description 服务实现类
 * @creatTime 2022-04-07 15:20:40
 */
@Service
public class MoperDutyMnExchangeServiceImpl extends ServiceImpl<MoperDutyMnExchangeMapper, MoperDutyMnExchange> implements IMoperDutyMnExchangeService {

    @Resource
    private MoperDutyMnExchangeMapper exchangeMapper;

    @Resource
    private MoperDutyMnRotaMapper moperDutyMnRotaMapper;

    @Resource
    private IMoperDutyMnRotaService moperDutyMnRotaService;

    @Autowired
    IMoperDutyMnClassesService moperDutyMnClassesService;

    /**
     * 分页搜索交换班记录
     * @param page
     * @param exchange
     * @return
     */
    @Override
    public IPage<MoperDutyMnExchange> selectExchangeRecordsByCondition(Page<MoperDutyMnExchange> page, MoperDutyMnExchangeVo exchange) {
        IPage<MoperDutyMnExchange> moperDutyMnExchangeIPage = exchangeMapper.selectExchangeRecordsByCondition(page, exchange);
        moperDutyMnExchangeIPage.getRecords().forEach(exchangeRecord ->{
            //获取班次名称
            String coamDutyClasses = exchangeRecord.getCoamDutyClasses();
            if(StrUtil.isNotEmpty(coamDutyClasses)){
                MoperDutyMnClasses classes = moperDutyMnClassesService.getById(coamDutyClasses);
                if(null!=classes){
                    exchangeRecord.setCoamDutyClassesName(classes.getCoamClassessName());
                }

            }
            //获取班次名称
            String coamDutyExchangeClasses = exchangeRecord.getCoamExchangeClasses();
            if(StrUtil.isNotEmpty(coamDutyExchangeClasses)){
                MoperDutyMnClasses classes = moperDutyMnClassesService.getById(coamDutyExchangeClasses);
                if(null!=classes){
                    exchangeRecord.setCoamExchangeClassesName(classes.getCoamClassessName());
                }

            }

        });
        return moperDutyMnExchangeIPage;
    }

    /**
     * 导出替班、换班记录
     * @param response
     * @param exchange
     */
    @Override
    public void exportExchangeRecords(HttpServletResponse response, MoperDutyMnExchangeVo exchange) throws IOException {
        List<MoperDutyMnExchange> exchanges = exchangeMapper.getExportExchangeByCondition(exchange);
        List<MoperDutyMnExchangeExcel>  exchangeExcels = new ArrayList<>();
        exchanges.forEach(change ->{
            MoperDutyMnExchangeExcel excel = new MoperDutyMnExchangeExcel();
            BeanUtils.copyProperties(change,excel);

            excel.setCoamExchangeStatus(change.getCoamExchangeStatus());
            //获取班次名称
            String coamDutyClasses = change.getCoamDutyClasses();
            if(StrUtil.isNotEmpty(coamDutyClasses)){
                MoperDutyMnClasses classes = moperDutyMnClassesService.getById(coamDutyClasses);
                excel.setCoamDutyClasses(classes.getCoamClassessName());
            }
            //获取班次名称
            String coamDutyExchangeClasses = change.getCoamExchangeClasses();
            if(StrUtil.isNotEmpty(coamDutyExchangeClasses)){
                MoperDutyMnClasses classes = moperDutyMnClassesService.getById(coamDutyExchangeClasses);
                excel.setCoamExchangeClasses(classes.getCoamClassessName());
            }
            exchangeExcels.add(excel);
        });
        String fileName = "替班换班记录.xlsx";
        HttpResponseUtil.setResponseHeaders(response, fileName);
        EasyExcel.write(response.getOutputStream(), MoperDutyMnExchangeExcel.class)
                .sheet("替班换班记录")
                .doWrite(() -> exchangeExcels);
    }

    /**
     * @author: wuxx
     * @date: 2022/4/24
     * @description: 审核交换班信息
     * @param exchange
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> examineRecord(MoperDutyMnExchange exchange) {
        if(null!=exchange){
            String coamKid = exchange.getCoamKid();
            MoperDutyMnExchange moperDutyMnExchange = exchangeMapper.selectById(coamKid);
            if(null == moperDutyMnExchange){
                return Result.failure("调换班记录不存在！");
            }
            //调班记录状态，0草稿，1审核通过，2审核不通过，3已提交
            Integer coamExchangeStatus = exchange.getCoamExchangeStatus();
            //审核通过修改交换班信息，审核不通过不处理
            if (1 == coamExchangeStatus.intValue()){
                //调换班类型(0:调班 1:替班)
                Integer coamExchangeType = moperDutyMnExchange.getCoamExchangeType();
                //替班，之前记录，替换新的
                if(1 == coamExchangeType.intValue()){
                    MoperDutyMnRota rota = moperDutyMnRotaService.getOne(new LambdaQueryWrapper<MoperDutyMnRota>()
                            .eq(MoperDutyMnRota::getCoamClassesId, moperDutyMnExchange.getCoamDutyClasses())
                            .eq(MoperDutyMnRota::getCoamGenerateDate, moperDutyMnExchange.getCoamDutyDate())
                            .eq(MoperDutyMnRota::getCoamDutyPersonId, moperDutyMnExchange.getCoamDutyPerson()));
                    if(null!=rota){
                        rota.setCoamDutyPersonId(moperDutyMnExchange.getCoamExchangePerson());
                        if(moperDutyMnRotaMapper.updateById(rota)>0){
                            return Result.content(this.updateById(exchange));
                        }
                        return Result.failure("替班记录不存在！");
                    }else {
                        return Result.failure("替班记录不存在！");
                    }
                }else if(0 == coamExchangeType.intValue()){
                    //值班人记录
                    MoperDutyMnRota rota =moperDutyMnRotaService.getOne(new LambdaQueryWrapper<MoperDutyMnRota>()
                            .eq(MoperDutyMnRota::getCoamClassesId, moperDutyMnExchange.getCoamDutyClasses())
                            .eq(MoperDutyMnRota::getCoamGenerateDate, moperDutyMnExchange.getCoamDutyDate())
                            .eq(MoperDutyMnRota::getCoamDutyPersonId, moperDutyMnExchange.getCoamDutyPerson()));
                    if(null == rota){
                        return Result.failure("值班记录不存在！");
                    }
                    String coamDutyPersonId = rota.getCoamDutyPersonId();

                    //换班人记录
                    MoperDutyMnRota changeRota = moperDutyMnRotaService.getOne(new LambdaQueryWrapper<MoperDutyMnRota>()
                            .eq(MoperDutyMnRota::getCoamClassesId, moperDutyMnExchange.getCoamExchangeClasses())
                            .eq(MoperDutyMnRota::getCoamGenerateDate, moperDutyMnExchange.getCoamExchangeDate())
                            .eq(MoperDutyMnRota::getCoamDutyPersonId, moperDutyMnExchange.getCoamExchangePerson()));
                    if(null == changeRota){
                        return Result.failure("换班记录不存在！");
                    }
                    String coamDutyChangePersonId = changeRota.getCoamDutyPersonId();
                    //交换值班人
                    rota.setCoamDutyPersonId(coamDutyChangePersonId);
                    changeRota.setCoamDutyPersonId(coamDutyPersonId);
                    if(moperDutyMnRotaMapper.updateById(rota)>0 && moperDutyMnRotaMapper.updateById(changeRota)>0 ){
                        this.updateById(exchange);
                        return Result.content(this.updateById(exchange));
                    }
                }
                return Result.failure("换班记录不存在！");
            }else{
                this.updateById(exchange);
            }

        }
        return Result.content(true);
    }

}
